//
//  Created by Anna Sannikova on 7/6/11.
//  Copyright 2011. All rights reserved.
//

#import "MystemmerAppDelegate.h"
#import "ExecutorThread.h"

@implementation MystemmerAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString *pathToFile = @"/tmp/mystemmerInput";
    
//    NSString *inputText = [NSString stringWithContentsOfFile:pathToFile encoding:NSWindowsCP1251StringEncoding error:NULL];
    NSLog(@"input text received");
    
    [self launchLemmatizerWithInputFile:pathToFile andOutputFile:@"/tmp/temporaryFile"];
    
    NSLog(@"called for mystem");
    
}


- (void)taskTerminated:(NSNotification *)notification
{
    NSLog(@"text mystemmed");

    NSString *mystemOutput = [NSString stringWithContentsOfFile:@"/tmp/temporaryFile" 
                                                       encoding:NSUTF8StringEncoding 
                                                          error:NULL];
    mystemOutput = [mystemOutput lowercaseString];
    
    //   NSLog(@"mystem output: %@", mystemOutput);
    
    NSLog(@"started to clean mystem output");
    
    NSString *cleanMystemOutput = [self cleanLemmatizerOutput:mystemOutput];
    
    [cleanMystemOutput writeToFile:@"/tmp/mystemmerOutput" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
    
//    NSLog(@"%@", cleanMystemOutput);
    
    NSLog(@"mystem output cleaned!");
    
    [NSApp terminate:nil];
}


#pragma mark -

- (void)launchLemmatizerWithInputFile:(NSString *)inputFilePath andOutputFile:(NSString *)outputFilePath
{
    m_task = [NSTask new];
    [m_task setLaunchPath:[[NSBundle mainBundle] pathForResource:@"mystem" ofType:@""]];
    [m_task setArguments:[NSArray arrayWithObjects:@"-c", @"-l", @"-e", @"utf-8", inputFilePath, outputFilePath, nil]];
    [m_task setStandardInput:[NSPipe pipe]];
    [m_task setStandardOutput:[NSPipe pipe]];
    [m_task launch];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(taskTerminated:)
                                                 name:NSTaskDidTerminateNotification
                                               object:nil];
}


#pragma mark -


- (NSString *)cleanLemmatizerOutput:(NSString *)mystemOutput
{

    NSMutableString *cleanMystemOutput = [NSMutableString new];
    
    BOOL isInsideCurlyBrackets = NO;
    BOOL lemmaEnded = NO;

    for (NSUInteger i = 0; i < [mystemOutput length]; i++) 
    {
        unichar letter = [mystemOutput characterAtIndex:i];
        
        if (letter == '{') 
        {
            isInsideCurlyBrackets = YES;
            lemmaEnded = NO;
            continue;
        }
        
        if (!isInsideCurlyBrackets && letter != '{' && letter != '|') 
        {
            [cleanMystemOutput appendFormat:@"%C", letter];
        }

        if (isInsideCurlyBrackets && !lemmaEnded) 
        {
            if (letter == '|') 
            {
                lemmaEnded = YES;
            }
            if (letter == '}') 
            {
                lemmaEnded = YES;
                isInsideCurlyBrackets = NO;
            }
            if (letter != '}' && letter != '|' && letter != '?') 
            {
                [cleanMystemOutput appendFormat:@"%C", letter];
            }
        }
        
        if (isInsideCurlyBrackets)
            if (letter == '}') isInsideCurlyBrackets = NO;
        
    }
    
    NSLog(@"CLEANING FINISHED");
    
    return cleanMystemOutput;
    
}

#pragma mark -

@end
